﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common.XmlHandle
{
    public class SearchData
    {
        private XmlDocument xdoc = new XmlDocument();
        private string xmlPath = null;
        List<ClassData> data = new List<ClassData>();

        public SearchData(string path)
        {
            this.xmlPath = path;
            if (!File.Exists(this.xmlPath))
            {
                //new XmlData();
            }
            xdoc.Load(xmlPath);
        }

        ///////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////XML文件ID（排号）的操作/////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////
        public ClassData sID(string year, int ID)
        {
            ClassData b = new ClassData();
            if (!File.Exists(this.xmlPath))
            {
                return null;
            }
            XmlNode root = xdoc.SelectSingleNode("Data" + year);

            XmlNodeList nodes = root.ChildNodes;
            foreach (XmlNode node in nodes)
            {
                XmlElement nod = (XmlElement)node;
                if (Int32.Parse(nod.GetAttribute("ID")) == ID)
                {
                    b = this.getData(node);
                    return b;
                }
            }
            return null;
        }

        public int getLastID()
        {
            string year = DateTime.Now.ToString().Substring(0, 4);
            XmlNode root = xdoc.SelectSingleNode("Data" + year);
            if (root == null)
            {
                return 0;
            }
            else
            {
                XmlNodeList nodes = root.ChildNodes;
                return nodes.Count;
            }
        }


        ///////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////XML文件Code（编码）的操作///////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////
        public List<ClassData> sCode(string year, string code)
        {
            List<ClassData> b = new List<ClassData>();
            if (!File.Exists(this.xmlPath))
            {
                return null;
            }
            XmlNode root = xdoc.SelectSingleNode("Data" + year);
            if (root == null)
            {
                return null;
            }
            XmlNodeList nodes = root.ChildNodes;
            foreach (XmlNode node in nodes)
            {
                XmlElement nod = (XmlElement)node;
                string xcode = nod.GetAttribute("Code");
                if (xcode.IndexOf(code) > -1)
                {
                    b.Add(getData(node));
                }
            }
            return b;
        }

        public int sCodeNumber(string year, string code)
        {
            List<ClassData> list = new List<ClassData>();
            int yearNow = Int32.Parse(DateTime.Now.ToString().Substring(0, 4));
            int yearHappen = Int32.Parse(year);
            for (int i = 0; i <= yearNow - yearHappen; i++)
            {
                string yearr = (yearHappen + i).ToString();
                if (sCode(yearr, code) == null)
                {
                    continue;
                }
                foreach (ClassData dat in sCode(yearr, code))
                {
                    list.Add(dat);
                }
            }
            return list.Count;
        }

        ///////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////XML文件获取节点数据的操作///////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////
        private ClassData getData(XmlNode node)
        {
            ClassData b = new ClassData();
            //b.Vehicles = new List<ClassVehicle>();
            XmlElement xele = (XmlElement)node;
            b.ID = Int32.Parse(xele.GetAttribute("ID"));
            b.Code = xele.GetAttribute("Code");
            XmlNodeList nods = node.ChildNodes;
            XmlElement xel1 = (XmlElement)nods[0];
            b.CaseType = Int32.Parse(xel1.GetAttribute("CaseType"));
            b.Brigade = new Guid(xel1.GetAttribute("Brigade"));
            b.Contact = xel1.GetAttribute("Contact");
            b.User = new Guid(xel1.GetAttribute("User"));
            b.TimeHappen = DateTime.Parse(xel1.GetAttribute("TimeHappen"));
            b.TimeArrange = DateTime.Parse(xel1.GetAttribute("TimeArrange"));
            XmlNodeList nos = nods[1].ChildNodes;
            foreach (XmlNode no in nos)
            {
                ClassVehicle veh = new ClassVehicle();
                XmlElement car = (XmlElement)no;
                veh.Number = car.GetAttribute("Number");
                veh.Brand = car.GetAttribute("Brand");
                veh.Type = car.GetAttribute("Type");
                b.Vehicles.Add(veh);
            }
            return b;
        }
    }
}
